package com.tzk.io;

import javax.swing.plaf.metal.MetalIconFactory;
import java.io.*;
import java.util.Scanner;

public class Ex01 {

    public static void main(String[] args) {
        new Ex01().copyFile();
        new Ex01().addString();
    }

    public void copyFile() {
        FileReader file1 = null;
        FileWriter file2 = null;
        try {
            file1 = new FileReader("d:/a.txt");
            file2 = new FileWriter("d:/b.txt");
            int d = 0;
            while ((d = file1.read()) != -1) {
                file2.write(d);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (file1 != null && file2 != null) {
                try {
                    file1.close();
                    file2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public void addString() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入添加内容");
        String add = sc.next();
        FileWriter file1 =null;
        try {
            file1 = new FileWriter("d:/a.txt", true);
            file1.write(add);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (file1!=null){
                try {
                    file1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

